package cn.iocoder.yudao.module.project.controller.admin.deliveryreceipt;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.project.controller.admin.deliveryreceipt.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.deliveryreceipt.DeliveryReceiptDO;
import cn.iocoder.yudao.module.project.service.deliveryreceipt.DeliveryReceiptService;

@Tag(name = "管理后台 - 产品拨交单")
@RestController
@RequestMapping("/project/delivery-receipt")
@Validated
public class DeliveryReceiptController {

    @Resource
    private DeliveryReceiptService deliveryReceiptService;

    @PostMapping("/create")
    @Operation(summary = "创建产品拨交单")
    //@PreAuthorize("@ss.hasPermission('project:delivery-receipt:create')")
    public CommonResult<Integer> createDeliveryReceipt(@Valid @RequestBody DeliveryReceiptSaveReqVO createReqVO) {
        return success(deliveryReceiptService.createDeliveryReceipt(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品拨交单")
    //@PreAuthorize("@ss.hasPermission('project:delivery-receipt:update')")
    public CommonResult<Boolean> updateDeliveryReceipt(@Valid @RequestBody DeliveryReceiptSaveReqVO updateReqVO) {
        deliveryReceiptService.updateDeliveryReceipt(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品拨交单")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('project:delivery-receipt:delete')")
    public CommonResult<Boolean> deleteDeliveryReceipt(@RequestParam("id") Integer id) {
        deliveryReceiptService.deleteDeliveryReceipt(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品拨交单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('project:delivery-receipt:query')")
    public CommonResult<DeliveryReceiptRespVO> getDeliveryReceipt(@RequestParam("id") Integer id) {
        DeliveryReceiptDO deliveryReceipt = deliveryReceiptService.getDeliveryReceipt(id);
        return success(BeanUtils.toBean(deliveryReceipt, DeliveryReceiptRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品拨交单分页")
    //@PreAuthorize("@ss.hasPermission('project:delivery-receipt:query')")
    public CommonResult<PageResult<DeliveryReceiptRespVO>> getDeliveryReceiptPage(@Valid DeliveryReceiptPageReqVO pageReqVO) {
        PageResult<DeliveryReceiptDO> pageResult = deliveryReceiptService.getDeliveryReceiptPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeliveryReceiptRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品拨交单 Excel")
    @PreAuthorize("@ss.hasPermission('project:delivery-receipt:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeliveryReceiptExcel(@Valid DeliveryReceiptPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeliveryReceiptDO> list = deliveryReceiptService.getDeliveryReceiptPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品拨交单.xls", "数据", DeliveryReceiptRespVO.class,
                        BeanUtils.toBean(list, DeliveryReceiptRespVO.class));
    }

    @PostMapping("/generateDeliveryReceipt")
    @Operation(summary = "生成产品拨交单")
    //@PreAuthorize("@ss.hasPermission('project:delivery-receipt:create')")
    public CommonResult<Integer> generateDeliveryReceipt(@RequestBody DeliveryReceiptConditionVO deliveryReceiptConditionVO) {
        return success(deliveryReceiptService.generateDeliveryReceipt(deliveryReceiptConditionVO));
    }

    @GetMapping("/getDeliveryReceiptReport")
    @Operation(summary = "获得产品拨交单报表")
    public CommonResult<PageResult<DeliveryReceiptRespVO>> getDeliveryReceiptReport(@Valid DeliveryReceiptPageReqVO pageReqVO) {
        PageResult<DeliveryReceiptDO> pageResult = deliveryReceiptService.getDeliveryReceiptReport(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeliveryReceiptRespVO.class));
    }

    @GetMapping("/exportDeliveryReceiptReport")
    @Operation(summary = "导出产品拨交单 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeliveryReceiptReport(@Valid DeliveryReceiptPageReqVO pageReqVO,
                                           HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeliveryReceiptDO> list = deliveryReceiptService.getDeliveryReceiptReport(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品拨交单.xls", "数据", DeliveryReceiptReportVO.class,
                BeanUtils.toBean(list, DeliveryReceiptReportVO.class));
    }

}